﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //链接kepserver的路径，不能更改
    QString url = "opc.tcp://127.0.0.1:49320";

    /*
     * 这是没有加线程的opcUA实例，不使用
    my_OpcUA = new OpcUA();
    if( !my_OpcUA->OpcUA_Init(url) )
    {
        (new QMessageBox(QMessageBox::Warning, u8"警告", u8"链接kepserver失败", QMessageBox::Ok, this))->show();
    }*/

    my_OpcUA_QThread = new OpcUA_QThread(this);
    connect(my_OpcUA_QThread, &OpcUA_QThread::signals_initResult, this, &MainWindow::on_opcUAInitFinished);
    my_OpcUA_QThread->start();
    my_OpcUA_QThread->init(url);

    my_homePage = new HomePage(my_OpcUA_QThread, this);
    my_flowchart = new FlowChart(my_OpcUA_QThread, this);

    connect(my_flowchart, &FlowChart::signals_flowChartStart, my_homePage, &HomePage::on_FlowChart_Start);
    connect(my_flowchart, &FlowChart::signals_flowChartStop, my_homePage, &HomePage::on_FlowChart_Stop);
    connect(my_flowchart, &FlowChart::signals_sendDesiredValue, my_homePage, &HomePage::on_receiveDesiredValue);
    connect(my_homePage, &HomePage::signals_sendTemperature, my_flowchart, &FlowChart::on_receiveTemperature);

    my_setPage = new SetPage(my_OpcUA_QThread, this);
    my_temperaturePage = new TemperaturePage(my_OpcUA_QThread, this);
    my_mksPage = new MKSPage(my_OpcUA_QThread, this);
    my_valvePage = new ValvePage(my_OpcUA_QThread, this);
    my_changePage = new ChangePage(my_OpcUA_QThread, this);
    my_dataPage = new DataPage(my_OpcUA_QThread, this);
    my_errorPage = new ErrorPage(my_OpcUA_QThread,this);

    connect(my_flowchart, &FlowChart::logOutput, this, &MainWindow::onLogOutput );

    ui->stackedWidget->addWidget(my_homePage);
    ui->stackedWidget->addWidget(my_flowchart);
    ui->stackedWidget->addWidget(my_dataPage);
    ui->stackedWidget->addWidget(my_errorPage);
    ui->stackedWidget->addWidget(my_setPage);
    ui->stackedWidget->addWidget(my_temperaturePage);
    ui->stackedWidget->addWidget(my_mksPage);
    ui->stackedWidget->addWidget(my_valvePage);
    ui->stackedWidget->addWidget(my_changePage);

    ui->stackedWidget->setCurrentWidget(my_homePage);

    qDebug() << u8"总页面数量：" << ui->stackedWidget->count();

    //加载外部QSS
    QFile file(":/qss/style.qss");
        if (!file.exists()) {
            qDebug() << u8"QSS文件不存在：" << ":/qss/style.qss";
            return;
        }
        if (file.open(QFile::ReadOnly | QFile::Text)) {
            QString styleSheet = QTextStream(&file).readAll();
            qApp->setStyleSheet(styleSheet);
            file.close();
        } else {
            qDebug() << u8"无法打开QSS文件：" << file.errorString();
        }

    //创建按钮组并设置排他模式
    QButtonGroup* buttonGroup = new QButtonGroup(this);
    buttonGroup->setExclusive(true); // 确保只有一个按钮被选中

    //将现有按钮添加到按钮组
    buttonGroup->addButton(ui->pushButton_HomePage);
    buttonGroup->addButton(ui->pushButton_Flowchart);
    buttonGroup->addButton(ui->pushButton_DataPage);
    buttonGroup->addButton(ui->pushButton_ErrorPage);
    buttonGroup->addButton(ui->pushButton_SetPage);
    buttonGroup->addButton(ui->pushButton_MKSpage);
    buttonGroup->addButton(ui->pushButton_ValvePage);
    buttonGroup->addButton(ui->pushButton_ChangePage);
    buttonGroup->addButton(ui->pushButton_Temperature);

    //设置按钮可被选中
    ui->pushButton_HomePage->setCheckable(true);
    ui->pushButton_Flowchart->setCheckable(true);
    ui->pushButton_DataPage->setCheckable(true);
    ui->pushButton_ErrorPage->setCheckable(true);
    ui->pushButton_SetPage->setCheckable(true);
    ui->pushButton_MKSpage->setCheckable(true);
    ui->pushButton_ValvePage->setCheckable(true);
    ui->pushButton_ChangePage->setCheckable(true);
    ui->pushButton_Temperature->setCheckable(true);

    //设置默认选中第一个按钮
    ui->pushButton_HomePage->setChecked(true);
}

MainWindow::~MainWindow()
{
    if( my_OpcUA_QThread )
        delete my_OpcUA_QThread;
    delete ui;
}

void MainWindow::on_pushButton_HomePage_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_Flowchart_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void MainWindow::on_pushButton_DataPage_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
}

void MainWindow::on_pushButton_ErrorPage_clicked()
{
    ui->stackedWidget->setCurrentIndex(5);
}

void MainWindow::on_pushButton_SetPage_clicked()
{
    //ui->stackedWidget->setCurrentIndex(6);
}

void MainWindow::on_pushButton_Temperature_clicked()
{
    //ui->stackedWidget->setCurrentIndex(7);
}

void MainWindow::on_pushButton_MKSpage_clicked()
{
    //ui->stackedWidget->setCurrentIndex(8);
}

void MainWindow::on_pushButton_ValvePage_clicked()
{
    //ui->stackedWidget->setCurrentIndex(9);
}

void MainWindow::on_pushButton_ChangePage_clicked()
{
    //ui->stackedWidget->setCurrentIndex(10);
}



void MainWindow::onLogOutput(QString log)
{
    QString myLog = QDateTime::currentDateTime().toString("yyyy:MM:dd hh:mm:ss :");
    ui->textEdit->append(myLog + log);
}

void MainWindow::on_opcUAInitFinished(bool success)
{
    if(!success)
    {
        QString myLog = QDateTime::currentDateTime().toString("yyyy:MM:dd hh:mm:ss :");
        qDebug() << myLog << u8"设备连接失败！";
        ui->textEdit->append(myLog + u8"设备连接失败！");
    }
    else
    {
        QString myLog = QDateTime::currentDateTime().toString("yyyy:MM:dd hh:mm:ss :");
        qDebug() << myLog << u8"设备连接成功！";
        ui->textEdit->append(myLog + u8"设备连接成功！");
    }
}
